home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_12_01 / smith / sfloat.cpp < prev    next >
C/C++ Source or Header  |  1993-11-08  |  3KB  |  126 lines

  1. // SFLOAT.CPP, Listing 3
  2.  
  3. #include <sfloat.hpp>
  4.  
  5. // Work space
  6. unsigned short sfloat::Exp;
  7. unsigned short sfloat::Man;
  8.  
  9. // Bit masks to extract parts of ieee float
  10. unsigned long sfloat::fManSignMask = 0x80000000L;
  11. unsigned long sfloat::fExpMask = 0x7F800000L;
  12. unsigned long sfloat::fManMask = 0x007FFFFFL;
  13.  
  14. // Bit mask to extract parts of short float
  15. unsigned short sfloat::sfManSignMask = 0x8000;
  16.  
  17. // float exponent bias
  18. unsigned short sfloat::fBias = 127;
  19.  
  20. // short float exponent bias
  21. unsigned short sfloat::sfBias = 127;
  22.  
  23. // fBias - sfBias
  24. unsigned short sfloat::fsfBias = 0;
  25.  
  26. // if signed flag
  27. unsigned short sfloat::Signed = 1;
  28.  
  29. // number of float exponent bits
  30. unsigned short sfloat::fExpBits = 8;
  31.  
  32. // number of short float exponent bits
  33. unsigned short sfloat::sfExpBits = 8;
  34.  
  35. // number of float mantissa bits
  36. unsigned short sfloat::fManBits = 23;
  37.  
  38. // number of short float mantiss bits
  39. unsigned short sfloat::sfManBits = 7;
  40.  
  41. // number of float mantissa sign bits
  42. unsigned short sfloat::fManSignBits = 1;
  43.  
  44. // number of float mantissa sign bits
  45. unsigned short sfloat::sfManSignBits = 1;
  46.  
  47. // number of short float bits
  48. unsigned short sfloat::sfBits = 16;
  49.  
  50. // float mantissa shift
  51. unsigned short sfloat::fManShift1 = 9;
  52.  
  53. // float mantissa shift
  54. unsigned short sfloat::fManShift2 = 7;
  55.  
  56. // short float mantissa shift
  57. unsigned short sfloat::sfManShift = 16;
  58.  
  59. // float exponent shift
  60. unsigned short sfloat::fExpShift = 8;
  61.  
  62. // short float exponent shift
  63. unsigned short sfloat::sfExpShift = 8;
  64.  
  65. // short float exponent minimum bits
  66. unsigned short sfloat::sfExpBitsMin = 1;
  67.  
  68. // short float exponent maximum bits
  69. unsigned short sfloat::sfExpBitsMax = 8;
  70.  
  71. // union for conversion
  72. union conv sfloat::u;
  73.  
  74. void sfloatrange( unsigned short sfNumExpBits,
  75.       unsigned short sfSigned )
  76.    {
  77.  
  78.    // Set the number of short float exponent bits
  79.    sfloat::sfExpBits = sfNumExpBits;
  80.    if ( sfloat::sfExpBits > sfloat::sfExpBitsMax )
  81.       {
  82.       sfloat::sfExpBits = sfloat::sfExpBitsMax;
  83.       }
  84.    else if ( sfloat::sfExpBits <
  85.          sfloat::sfExpBitsMin )
  86.       {
  87.       sfloat::sfExpBits = sfloat::sfExpBitsMin;
  88.       }
  89.  
  90.    // Set the number of short float sign bits
  91.    if ( sfSigned )
  92.       {
  93.       sfloat::Signed = sfloat::sfManSignBits = 1;
  94.       }
  95.    else
  96.       {
  97.       sfloat::Signed = sfloat::sfManSignBits = 0;
  98.       }
  99.  
  100.    // Set the number of short float mantissa bits
  101.    sfloat::sfManBits = sfloat::sfBits - 
  102.          sfloat::sfManSignBits - sfloat::sfExpBits;
  103.  
  104.    // Set the short float exponent bias value
  105.    sfloat::sfBias = 0;
  106.    sfloat::sfBias =
  107.          ( 1 << ( sfloat::sfExpBits - 1 )) - 1;
  108.    sfloat::fsfBias = sfloat::fBias - sfloat::sfBias;
  109.  
  110.    // Set the converson shift values
  111.    sfloat::fManShift1 = sfloat::sfManSignBits +
  112.          sfloat::sfExpBits;
  113.    sfloat::fManShift2 = sfloat::sfBits - 
  114.          sfloat::fExpBits - sfloat::fManSignBits;
  115.    sfloat::sfManShift = sfloat::fManBits -
  116.          sfloat::sfBits + sfloat::sfExpBits +
  117.          sfloat::sfManSignBits;
  118.    sfloat::fExpShift = sfloat::sfManBits +
  119.          sfloat::sfManSignBits;
  120.    sfloat::sfExpShift = sfloat::sfBits -
  121.          sfloat::sfExpBits;
  122.  
  123.    }   // sfloatrange
  124.  
  125. // End SFLOAT.CPP
  126.